package controller;

import bean.AdminBean;
import bean.GeneralBean;
import bean.OrderBean;
import bean.UserBean;
import begin.AdminMenu;
import begin.MainView;
import dao.AdminDao;
import dao.GeneralDao;
import dao.OrderDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import service.AdminService;
import tools.UtilData;
import tools.UtilMessage;

import javax.swing.plaf.FileChooserUI;
import java.io.File;

/**
 * @Author 王志康
 * @Date 2021/9/7 11:08
 * @Version 1.0.0
 */
public class AdminMenuController {

    //商品列表

    @FXML
    private TableView<GeneralBean> itemListTable;
    @FXML
    private TableColumn<Object, Object> itemId;
    @FXML
    private TableColumn<Object, Object> itemName;
    @FXML
    private TableColumn<Object, Object> itemPrice;
    @FXML
    private TableColumn<Object, Object> onTheShelf;


    //管理员列表

    @FXML
    private TextField adminLevelText;
    @FXML
    private TextField adminPasswordText;
    @FXML
    private TextField adminNumberText;
    @FXML
    private TableView<AdminBean> adminListTable;
    @FXML
    private TableColumn<Object, Object> adminNumber;
    @FXML
    private TableColumn<Object, Object> adminPassword;
    @FXML
    private TableColumn<Object, Object> adminLevel;


    //订单列表

    @FXML
    private TextArea orderContentArea;
    @FXML
    private TableView<OrderBean> orderListTable;
    @FXML
    private TableColumn<Object, Object> orderId;
    @FXML
    private TableColumn<Object, Object> orderNumber;
    @FXML
    private TableColumn<Object, Object> orderTime;
    @FXML
    private TableColumn<Object, Object> orderIntegral;
    @FXML
    private TableColumn<Object, Object> orderContent;

    //用户列表

    @FXML
    private TableView<UserBean> userListTable;
    @FXML
    private TextField userNumberText;
    @FXML
    private TextField userNameText;
    @FXML
    private TextField userIntegralText;
    @FXML
    private TextField userPhoneText;
    @FXML
    private TextField userPasswordText;
    @FXML
    private TableColumn<Object, Object> userNumber;
    @FXML
    private TableColumn<Object, Object> userPassword;
    @FXML
    private TableColumn<Object, Object> userIntegral;
    @FXML
    private TableColumn<Object, Object> userName;
    @FXML
    private TableColumn<Object, Object> userPhone;

    /**
     * 跳转至用户列表时刷新用户列表数据
     *
     * @param event 事件
     */
    @FXML
    public void toUserList(Event event) {
        userNumber.setCellValueFactory(new PropertyValueFactory<>("number"));
        userPassword.setCellValueFactory(new PropertyValueFactory<>("password"));
        userIntegral.setCellValueFactory(new PropertyValueFactory<>("integral"));
        userName.setCellValueFactory(new PropertyValueFactory<>("userName"));
        userPhone.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        ObservableList<UserBean> list = FXCollections.observableArrayList();
        list.addAll(UserDao.getUserList());
        userListTable.setItems(list);
    }

    /**
     * 跳转至订单列表时刷新订单列表
     *
     * @param event 事件
     */
    public void toOrderList(Event event) {
        orderId.setCellValueFactory(new PropertyValueFactory<>("id"));
        orderNumber.setCellValueFactory(new PropertyValueFactory<>("userNumber"));
        orderContent.setCellValueFactory(new PropertyValueFactory<>("orderContent"));
        orderTime.setCellValueFactory(new PropertyValueFactory<>("orderTime"));
        orderIntegral.setCellValueFactory(new PropertyValueFactory<>("getIntegral"));
        ObservableList<OrderBean> list = FXCollections.observableArrayList();
        list.addAll(OrderDao.getUserList());
        orderListTable.setItems(list);
    }


    /**
     * 跳转至管理员列表时刷新管理员列表
     *
     * @param event 事件
     */
    public void toAdminList(Event event) {
        adminNumber.setCellValueFactory(new PropertyValueFactory<>("number"));
        adminPassword.setCellValueFactory(new PropertyValueFactory<>("password"));
        adminLevel.setCellValueFactory(new PropertyValueFactory<>("level"));
        ObservableList<AdminBean> list = FXCollections.observableArrayList();
        list.addAll(AdminDao.getAdminList());
        adminListTable.setItems(list);
    }

    /**
     * 跳转至商品列表时刷新管理员列表
     *
     * @param event 事件
     */
    public void toItemList(Event event) {
        itemId.setCellValueFactory(new PropertyValueFactory<>("productId"));
        itemName.setCellValueFactory(new PropertyValueFactory<>("productName"));
        itemPrice.setCellValueFactory(new PropertyValueFactory<>("productPrice"));
        onTheShelf.setCellValueFactory(new PropertyValueFactory<>("onTheShelf"));
        ObservableList<GeneralBean> list = FXCollections.observableArrayList();
        list.addAll(GeneralDao.getAllGeneralList());
        itemListTable.setItems(list);
    }

    private UserBean user;

    /**
     * 获取用户列表中选中的数据
     *
     * @param mouseEvent 鼠标事件
     */
    @FXML
    public void userTableClicked(MouseEvent mouseEvent) {
        user = userListTable.getSelectionModel().getSelectedItem();
        userNumberText.setText(user.getNumber());
        userPasswordText.setText(user.getPassword());
        userIntegralText.setText(String.valueOf(user.getIntegral()));
        userNameText.setText(user.getUserName());
        userPhoneText.setText(user.getPhoneNumber());
    }

    /**
     * 获取订单列表中选中的数据
     *
     * @param mouseEvent 鼠标事件
     */
    public void orderTableClicked(MouseEvent mouseEvent) {
        String content = orderListTable.getSelectionModel().getSelectedItem().getOrderContent();
        //设置自动换行
        orderContentArea.setWrapText(true);
        orderContentArea.setText(content);
    }

    /**
     * 获取管理员列表中选中的数据
     *
     * @param mouseEvent 鼠标事件
     */
    public void adminTableClicked(MouseEvent mouseEvent) {
        AdminBean admin = adminListTable.getSelectionModel().getSelectedItem();
        adminNumberText.setText(admin.getNumber());
        adminPasswordText.setText(admin.getPassword());
        adminLevelText.setText(String.valueOf(admin.getLevel()));
    }

    /**
     * 更新用户数据
     *
     * @param mouseEvent 鼠标事件
     */
    public void changeUser(MouseEvent mouseEvent) {
        String number = userNumberText.getText();
        String password = userPasswordText.getText();
        String integral = userIntegralText.getText();
        String name = userNameText.getText();
        String phone = userPhoneText.getText();
        if (!number.equals(user.getNumber())) {
            new UtilMessage().messageShow("请检查账号是否正确");
            return;
        }
        UserDao.updateUser(new UserBean(number, password, Integer.parseInt(integral), name, phone));
        new UtilMessage().messageShow("用户:" + number + " 修改成功");
        //更新用户列表
        toUserList(mouseEvent);
    }

    /**
     * 删除用户
     *
     * @param mouseEvent 鼠标事件
     */
    public void deleteUser(MouseEvent mouseEvent) {
        UserDao.deleteUser(user);
        new UtilMessage().messageShow("用户:" + user.getNumber() + " 删除成功");
        //更新用户列表
        toUserList(mouseEvent);
    }

    /**
     * 添加用户
     *
     * @param mouseEvent 鼠标事件
     */
    public void addUser(MouseEvent mouseEvent) {
        String number = userNumberText.getText();
        String password = userPasswordText.getText();
        int integral = Math.max(Integer.parseInt(userIntegralText.getText()), 0);
        String name = userNameText.getText();
        String phone = userPhoneText.getText();
        if (phone.length() != UtilData.TELEPHONE_NUMBER_DIGITS) {
            new UtilMessage().messageShow("电话号码错误,请检查");
            return;
        }
        AdminService.addUserService(new UserBean(number, password, integral, name, phone));
        //更新用户列表
        toUserList(mouseEvent);
    }


    /**
     * 删除订单
     *
     * @param mouseEvent 鼠标事件
     */
    public void deleteOrder(MouseEvent mouseEvent) {
        if (orderListTable.getSelectionModel().getSelectedIndex() == -1) {
            new UtilMessage().messageShow("请选中一个订单");
            return;
        }
        OrderBean order = orderListTable.getSelectionModel().getSelectedItem();
        OrderDao.deleteOrder(order);
        //更新订单列表
        toAdminList(mouseEvent);
    }

    /**
     * 更新管理员数据
     *
     * @param mouseEvent 鼠标事件
     */
    public void changeAdmin(MouseEvent mouseEvent) {
        String number = adminNumberText.getText();
        String password = adminPasswordText.getText();
        if (AdminDao.verification(new AdminBean(number))) {
            new UtilMessage().messageShow("查无此管理员");
            return;
        }
        int level = AdminDao.getLevel(number);
        if (level > AdminMenu.admin.getLevel() ||
                Integer.parseInt(adminLevelText.getText()) > AdminMenu.admin.getLevel()) {
            new UtilMessage().messageShow("权限等级不足");
            return;
        }
        AdminDao.updateAdmin(new AdminBean(number, password, Integer.parseInt(adminLevelText.getText())));
        new UtilMessage().messageShow("修改成功");
        //更新管理员列表
        toAdminList(mouseEvent);
    }

    /**
     * 添加管理员
     *
     * @param mouseEvent 鼠标事件
     */
    public void addAdmin(MouseEvent mouseEvent) {
        String number = adminNumberText.getText();
        String password = adminPasswordText.getText();
        int level = Math.max(Integer.parseInt(adminLevelText.getText()), 1);
        if (level > AdminMenu.admin.getLevel()) {
            new UtilMessage().messageShow("权限等级不足");
            return;
        }
        AdminService.addAdminService(new AdminBean(number, password, level));
        //更新管理员列表
        toAdminList(mouseEvent);

    }

    /**
     * 删除管理员
     *
     * @param mouseEvent 鼠标事件
     */
    public void deleteAdmin(MouseEvent mouseEvent) {
        if (adminListTable.getSelectionModel().getSelectedIndex() == -1) {
            new UtilMessage().messageShow("请选中一位管理员");
            return;
        }
        AdminBean admin = adminListTable.getSelectionModel().getSelectedItem();
        if (admin.getLevel() > AdminMenu.admin.getLevel()) {
            new UtilMessage().messageShow("权限等级不足");
            return;
        }
        AdminDao.deleteAdmin(admin);
        new UtilMessage().messageShow("删除管理员成功");
        //更新管理员列表
        toAdminList(mouseEvent);
    }

    /**
     * 商品上架
     *
     * @param mouseEvent 鼠标事件
     */
    public void putTheShelf(MouseEvent mouseEvent) {
        if (itemListTable.getSelectionModel().getSelectedIndex() == -1) {
            new UtilMessage().messageShow("请选中一件商品");
            return;
        }
        int id = itemListTable.getSelectionModel().getSelectedItem().getProductId();
        GeneralDao.setOnTheShelfTrue(id);
        toItemList(mouseEvent);
        new UtilMessage().messageShow("商品上架成功");
    }

    /**
     * 商品下架
     *
     * @param mouseEvent 鼠标事件
     */
    public void offTheShelf(MouseEvent mouseEvent) {
        if (itemListTable.getSelectionModel().getSelectedIndex() == -1) {
            new UtilMessage().messageShow("请选中一件商品");
            return;
        }
        int id = itemListTable.getSelectionModel().getSelectedItem().getProductId();
        GeneralDao.setOnTheShelfFalse(id);
        toItemList(mouseEvent);
        new UtilMessage().messageShow("商品下架成功");
    }

    /**
     * 删除商品
     *
     * @param mouseEvent 鼠标事件
     */
    public void deleteItem(MouseEvent mouseEvent) {
        if (itemListTable.getSelectionModel().getSelectedIndex() == -1) {
            new UtilMessage().messageShow("请选中一件商品");
            return;
        }
        int id = itemListTable.getSelectionModel().getSelectedItem().getProductId();
        GeneralDao.deleteGeneral(id);
        toItemList(mouseEvent);
        new UtilMessage().messageShow("商品删除成功");
    }

    /**
     * 打开文件管理器并选择导入的表格文件
     *
     * @param mouseEvent 鼠标事件
     */
    public void openFileChoose(MouseEvent mouseEvent) {
        AdminService.importDate();
        toItemList(mouseEvent);
    }

    /**
     * 系统退出函数
     *
     * @param mouseEvent 鼠标事件
     */
    public void exit(MouseEvent mouseEvent) {
        System.exit(0);
    }

    /**
     * 账号注销函数
     *
     * @param mouseEvent 鼠标事件
     */
    public void cancellation(MouseEvent mouseEvent) {
        Stage stage = (Stage) adminLevelText.getScene().getWindow();
        stage.close();
        new MainView().gotoMainView();
    }
}
